package com.bonana.core.services.impl;

import com.bonana.core.dao.user.UserAlbumMapper;
import com.bonana.core.entity.mybatis.UserAlbum;
import com.bonana.core.entity.mybatis.UserAlbumExample;
import com.bonana.core.services.AlbumService;
import com.github.pagehelper.PageHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2016/7/11.
 */
@Service
public class AlbumServiceImpl  implements AlbumService{
    private Logger logger = LogManager.getLogger(AlbumServiceImpl.class);
    @Autowired
    UserAlbumMapper albumMapper;
    @Override
    public int RemoveAlbumById(int albumId) {
        logger.info("RemoveAlbumById:{}", albumId);
        return albumMapper.deleteByPrimaryKey(albumId);
    }

    @Override
    public int AddAlbum(UserAlbum userAlbum) {
        logger.info("AddAlbum:{} {}", userAlbum.getUserId(), userAlbum.getUrl());
        int ret = albumMapper.insert(userAlbum);
        return ret>0 ? userAlbum.getAlbumId() : 0;
    }

    @Override
    public List<UserAlbum> getUserAlbum(int uid, int pgSize, int pgNum){
        logger.info("getUserAlbum: uid:{} pgSize:{} pgNum:{}", uid, pgSize, pgNum);
        PageHelper.startPage(pgNum, pgSize);
        UserAlbumExample ex = new UserAlbumExample();
        ex.createCriteria().andUserIdEqualTo(uid);
        ex.setOrderByClause("create_time desc");
        return albumMapper.selectByExample(ex);
    }
}
